/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import models.Activation;
import models.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Achie
 */
public class ActivationDao {

    private final Session session;

    public ActivationDao(Session session) {
        this.session = session;
    }

    public List<Activation> findAll() {
        Transaction tx = session.beginTransaction();
        List<Activation> list = session.createQuery("from Activation").list();
        tx.commit();
        return list;
    }

    public boolean save(Activation activation) {
        try {
            Transaction tx = session.beginTransaction();
            session.save(activation);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean update(Activation activation) {
        try {
            Transaction tx = session.beginTransaction();
            session.update(activation);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public Activation get(long id) {
        return (Activation) session.get(Activation.class, id);
    }

     public List<Activation> findByKey(String key) { //get activation objects by key
        Transaction tx = session.beginTransaction();
        List<Activation> list = session.createQuery("from Activation where activation_key= :key").setParameter("key", key).list();
        tx.commit();
        return list;
    }
    
    public Activation findByStudentId(long studentid) { //get student by student id
        Transaction tx = session.beginTransaction();
        List<Activation> list = session.createQuery("from Activation where student_id= :id").setParameter("id", studentid).list();
        tx.commit();
        return list.get(0);
    }
    
    public void delete(Activation activation) {
        Transaction tx = session.beginTransaction();
        session.delete(activation);
        tx.commit();
    }
    
}
